package cn.aran.filter;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Component
public class CustomFilter extends ZuulFilter {

    /**
    * @Title: filterType
    * @Description: filter的类型
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 22:43
    * @Parameters: []
    * @Return java.lang.String
    */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
    * @Title: filterOrder
    * @Description: filter的执行顺序，越小越先执行
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 22:44
    * @Parameters: []
    * @Return int
    */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
    * @Title: shouldFilter
    * @Description: 决定run方法是否要被执行
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 22:44
    * @Parameters: []
    * @Return boolean
    */
    @Override
    public boolean shouldFilter() {
        // 获取request对象,获取请求中的url
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        String uri = request.getRequestURI();
        //  判断是否包含在： static/**  ，/login , /register
        if(uri.endsWith("/login ") || uri.endsWith("/register ") || uri.contains("/static/") ){
            return false;
        }
        return true;
    }

    /**
    * @Title: run
    * @Description: 核心方法
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 22:45
    * @Parameters: []
    * @Return java.lang.Object
    */
    @Override
    public Object run() throws ZuulException {
        // 获取request对象,获取请求中的url
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        // 响应对象
        HttpServletResponse response = RequestContext.getCurrentContext().getResponse();
        // 获取请求头中的 token
        String token = request.getHeader("token");
        // 判断token
        if(StringUtils.isBlank(token)&&!tokenIsTrue(token)) {
            Map<String,Object> result = new HashMap<>();
            result.put("success" , false);
            result.put("message" , "未登录!");
            result.put("code", 402); // 状态码
            result.put("data", null); // 数据
            //中文编码
            response.setContentType("application/json;charset=utf-8");
            //把map转成json字符串，写到浏览器
            String resultJsonString = JSON.toJSONString(result);
            response.setStatus(402);
            try {
                response.getWriter().write(resultJsonString);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //拦截,阻止filter继续往后执行
            RequestContext.getCurrentContext().setSendZuulResponse(false);
        }
        return null;
    }

    /**
    * @Title: tokenIsTrue
    * @Description: 判断token
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/31 22:56
    * @Parameters: [token]
    * @Return boolean
    */
    private boolean tokenIsTrue(String token){
        return false;
    }
}
